package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.View
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.activity.QuanXainOrganizationActivity
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.ManagerViewPageAdapter
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.ProjectQueryActivityDelegate
import cc.vv.btpartyjob.partymanager.fragment.*
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 项目查询
 * Created by Chen.qi on 2018/10/29.
 */
class ProjectQueryActivity : BaseActivityMVP<ProjectQueryActivityDelegate>() {

    /**
     * ViewPager适配器
     */
    private var mAdapter: ManagerViewPageAdapter? = null

    /**
     * Fragment集合
     */
    private var mFragments: ArrayList<Fragment>? = null

    private var keyStr: String? = null

    private var pos: Int = 0

    override fun initData() {
        viewDelegate?.setTopBarTitle("项目查询")
        mFragments = ArrayList()
        val queryFragmentZC = ProjectQueryFragment_ZhengChang()
        val queryFragmentYJ = ProjectQueryFragment_YJ()
        val queryFragmentYQ = ProjectQueryFragment_YQ()
        val queryFragmentQX = ProjectQueryFragment_QX()
        val queryFragmentJX = ProjectQueryFragment_JX()
        mFragments!!.add(queryFragmentZC)
        mFragments!!.add(queryFragmentYJ)
        mFragments!!.add(queryFragmentYQ)
        mFragments!!.add(queryFragmentQX)
        mFragments!!.add(queryFragmentJX)
        mAdapter = ManagerViewPageAdapter(supportFragmentManager, mFragments!!)
        viewDelegate?.setAdapterData(mAdapter!!)
        viewDelegate?.setTextStatus(0)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                //项目搜索
//                if (viewDelegate?.getSearchShow()!!) {
//                    viewDelegate?.showSearchView(false)
//                } else {
//                    viewDelegate?.showSearchView(true)
//                }

                val intent = Intent(this@ProjectQueryActivity, QuanXainOrganizationActivity::class.java)
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 0)
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_SHIELD_UNIT, false)
                startActivityForResult(intent, 100)

//                when {
//                    UserRoleServer.getAreaRole() -> JniStartActivity.startIntentOrganActivity(this@ProjectQueryActivity, 0, 0, 100)
//                    UserRoleServer.getStreetRole() -> JniStartActivity.startIntentOrganActivity(this@ProjectQueryActivity, 1, 1, 100)
//                    UserRoleServer.getSheQuRole() -> JniStartActivity.startIntentOrganActivity(this@ProjectQueryActivity, 2, 2, 100)
//                    UserRoleServer.getGridRole() -> JniStartActivity.startIntentOrganActivity(this@ProjectQueryActivity, 3, 3, 100)
//                }
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //正常
            viewDelegate?.setViewPagerPos(0)

        }, R.id.tv_apq_projectNormal)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //预警
            viewDelegate?.setViewPagerPos(1)

        }, R.id.tv_apq_projectWarning)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //延期
            viewDelegate?.setViewPagerPos(2)

        }, R.id.tv_apq_projectPostpone)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //取消
            viewDelegate?.setViewPagerPos(3)

        }, R.id.tv_apq_projectCancel)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //结项
            viewDelegate?.setViewPagerPos(4)

        }, R.id.tv_apq_projectPDSP)


        viewDelegate?.bindViewPagerListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                viewDelegate?.setTextStatus(position)
                pos = position
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //空事件
            viewDelegate?.showSearchView(false)
        }, R.id.rl_apq_searchTop)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //搜索
            closeInput()
            keyStr = viewDelegate?.getInputSearchText()
            val fragment: ProjectQueryFragment
            try {
                fragment = mFragments!![pos] as ProjectQueryFragment
                fragment.search(keyStr)
            } catch (e: Exception) {
            }

            viewDelegate?.showSearchView(false)
        }, R.id.tv_apq_searchBtn)

    }

    override fun getDelegateClass(): Class<ProjectQueryActivityDelegate> {
        return ProjectQueryActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            val orgId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val type = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_TYPE)
            try {
                val zc = mFragments!![0] as ProjectQueryFragment_ZhengChang
                val yj = mFragments!![1] as ProjectQueryFragment_YJ
                val yq = mFragments!![2] as ProjectQueryFragment_YQ
                val qx = mFragments!![3] as ProjectQueryFragment_QX
                val jx = mFragments!![4] as ProjectQueryFragment_JX
                zc.zuZhiQuery(orgId, type)
                yj.zuZhiQuery(orgId, type)
                yq.zuZhiQuery(orgId, type)
                qx.zuZhiQuery(orgId, type)
                jx.zuZhiQuery(orgId, type)
            } catch (e: Exception) {
            }
        }

    }
}